home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / gxfill.c < prev    next >
C/C++ Source or Header  |  1993-05-13  |  24KB  |  719 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gxfill.c */
  20. /* Lower-level path filling procedures for Ghostscript library */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gxfixed.h"
  24. #include "gxmatrix.h"
  25. #include "gxdevice.h"            /* for gx_color_index */
  26. #include "gzcolor.h"
  27. #include "gzpath.h"
  28. #include "gzstate.h"
  29. #include "gxcpath.h"
  30.  
  31. /* Import the fixed * fixed / fixed routine from gxdraw.c. */
  32. /* The second argument must be less than or equal to the third; */
  33. /* all must be non-negative, and the last must be non-zero. */
  34. extern fixed fixed_mult_quo(P3(fixed, fixed, fixed));
  35.  
  36. /* Define the structure for keeping track of active lines. */
  37. typedef struct active_line_s active_line;
  38. struct active_line_s {
  39.     gs_fixed_point start;        /* x,y where line starts */
  40.     gs_fixed_point end;        /* x,y where line ends */
  41.     fixed dx;            /* end.x - start.x */
  42. #define al_dx(alp) ((alp)->dx)
  43. #define al_dy(alp) ((alp)->end.y - (alp)->start.y)
  44.     fixed y_fast_max;        /* can do x_at_y in fixed point */
  45.                     /* if y <= y_fast_max */
  46. #define set_al_points(alp, startp, endp)\
  47.   (alp)->dx = (endp).x - (startp).x,\
  48.   (alp)->y_fast_max = max_fixed /\
  49.     (((alp)->dx >= 0 ? (alp)->dx : -(alp)->dx) | 1) + (startp).y,\
  50.   (alp)->start = startp, (alp)->end = endp
  51. #define al_x_at_y(alp, yv)\
  52.   ((yv) == (alp)->end.y ? (alp)->end.x :\
  53.    ((yv) <= (alp)->y_fast_max ?\
  54.     ((yv) - (alp)->start.y) * al_dx(alp) / al_dy(alp) :\
  55.     (stat(n_slow_x),\
  56.      fixed_mult_quo(al_dx(alp), (yv) - (alp)->start.y, al_dy(alp)))) +\
  57.    (alp)->start.x)
  58.     fixed x_current;        /* current x position */
  59.     fixed x_next;            /* x position at end of band */
  60.     const segment *pseg;        /* endpoint of this line */
  61.     int direction;            /* direction of line segment */
  62. #define dir_up 1
  63. #define dir_down (-1)
  64. /* "Pending" lines (not reached in the Y ordering yet) use next and prev */
  65. /* to order lines by increasing starting Y.  "Active" lines (being scanned) */
  66. /* use next and prev to order lines by increasing current X, or if the */
  67. /* current Xs are equal, by increasing final X. */
  68.     active_line *prev, *next;
  69. /* Link together active_lines allocated individually */
  70.     active_line *alloc_next;
  71. };
  72.  
  73. /* Define the ordering criterion for active lines. */
  74. /* The xc argument is a copy of lp2->x_current. */
  75. #define x_precedes(lp1, lp2, xc)\
  76.   (lp1->x_current < xc || lp1->x_current == xc &&\
  77.    (lp1->start.x > lp2->start.x || lp1->end.x < lp2->end.x))
  78.  
  79. #ifdef DEBUG
  80. /* Internal procedures for printing active lines */
  81. private void
  82. print_active_line(char *label, active_line *alp)
  83. {    dprintf5("[f]%s %lx(%d): x_current=%f x_next=%f\n",
  84.              label, (ulong)alp, alp->direction,
  85.              fixed2float(alp->x_current), fixed2float(alp->x_next));
  86.     dprintf5("    start=(%f,%f) pt_end=%lx(%f,%f)\n",
  87.              fixed2float(alp->start.x), fixed2float(alp->start.y),
  88.              (ulong)alp->pseg,
  89.              fixed2float(alp->end.x), fixed2float(alp->end.y));
  90.     dprintf2("    prev=%lx next=%lx\n",
  91.          (ulong)alp->prev, (ulong)alp->next);
  92. }
  93. private void
  94. print_line_list(active_line *flp)
  95. {    active_line *lp;
  96.     for ( lp = flp; lp != 0; lp = lp->next )
  97.        {    fixed xc = lp->x_current, xn = lp->x_next;
  98.         dprintf3("[f]%lx(%d): x_current/next=%g",
  99.                  (ulong)lp, lp->direction,
  100.                  fixed2float(xc));
  101.         if ( xn != xc ) dprintf1("/%g", fixed2float(xn));
  102.         dputc('\n');
  103.        }
  104. }
  105. #define print_al(label,alp)\
  106.   if ( gs_debug['F'] ) print_active_line(label, alp)
  107. #else
  108. #define print_al(label,alp) 0
  109. #endif
  110.  
  111. /* Line list structure */
  112. struct line_list_s {
  113.     active_line *active_area;    /* allocated active_line list */
  114.     line_close_segment *close_area;    /* allocated closing line area */
  115.     uint close_count;
  116.     gs_fixed_rect box;        /* intersection of bounding boxes, */
  117.                     /* disregard lines outside this */
  118.     active_line *next_active;    /* next allocation slot */
  119.     active_line *limit;        /* limit of local allocation */
  120.     line_close_segment *next_line;    /* next line allocation slot */
  121.     active_line *y_list;        /* Y-sorted list of pending lines */
  122.     active_line *y_line;        /* most recently inserted line */
  123.     active_line x_head;        /* X-sorted list of active lines */
  124. #define x_list x_head.next
  125.         /* Put the arrays last so the scalars will have */
  126.         /* small displacements. */
  127.         /* Allocate a few active_lines and line_close_segments */
  128.         /* locally to avoid round trips through the allocator. */
  129. #define max_local_active 20
  130.     active_line local_active[max_local_active];
  131. #define max_local_close 5
  132.     line_close_segment local_close[max_local_close];
  133. };
  134. typedef struct line_list_s line_list;
  135. typedef line_list _ss *ll_ptr;
  136.  
  137. /* Forward declarations */
  138. private int alloc_line_list(P2(ll_ptr, uint));
  139. private void free_line_list(P1(ll_ptr));
  140. private int add_y_list(P2(gx_path *, ll_ptr));
  141. private int add_y_line(P4(const segment *, const segment *, int, ll_ptr));
  142. private int fill_loop(P5(const gx_device_color *, int, ll_ptr,
  143.   gs_state *, fixed));
  144. private void insert_x_new(P2(active_line *, ll_ptr));
  145. private int update_x_list(P2(active_line *, fixed));
  146.  
  147. /* Statistics */
  148. #ifdef DEBUG
  149. #define stat(x) (x++)
  150. #define statn(x,n) (x += (n))
  151. private long n_fill;
  152. private long n_fill_alloc;
  153. private long n_y_up;
  154. private long n_y_down;
  155. private long n_x_step;
  156. private long n_slow_x;
  157. private long n_iter;
  158. private long n_find_y;
  159. private long n_band;
  160. private long n_band_step;
  161. private long n_band_fill;
  162. #else
  163. #define stat(x) 0
  164. #define statn(x,n) 0
  165. #endif
  166.  
  167. /* General area filling algorithm. */
  168. /* The adjust parameter is a hack for keeping small characters */
  169. /* from coming out too faint: it specifies an amount by which to expand */
  170. /* all sides of every filled region. */
  171. int
  172. gx_fill_path(gx_path *ppath, gx_device_color *pdevc, gs_state *pgs,
  173.   int rule, fixed adjust)
  174. {    const gx_clip_path *pcpath = pgs->clip_path;
  175.     gs_fixed_rect cbox;
  176.     gx_path *pfpath;
  177.     gx_path ffpath;
  178.     int code;
  179.     line_list lst;
  180.     uint sub_count;
  181.     gx_device_clip cdev;
  182.     int do_clip;
  183.     /* Fatten everything a little to make it look better. */
  184.     /****** This is something of a hack. ******/
  185.     if ( adjust == 0 ) adjust = pgs->fill_adjust;
  186.     /* Start by flattening the path.  We should do this on-the-fly.... */
  187.     if ( !ppath->curve_count )    /* don't need to flatten */
  188.         pfpath = ppath;
  189.     else
  190.        {    if ( (code = gx_path_flatten(ppath, &ffpath, pgs->flatness, (int)pgs->in_cachedevice)) < 0 ) return code;
  191.         pfpath = &ffpath;
  192.        }
  193.     /* Check the bounding boxes. */
  194. #define ibox lst.box
  195.     gx_path_bbox(pfpath, &ibox);
  196.     gx_cpath_box_for_check(pcpath, &cbox);
  197.     if ( ibox.q.y <= cbox.q.y && ibox.q.x <= cbox.q.x &&
  198.          ibox.p.y >= cbox.p.y && ibox.p.x >= cbox.p.x
  199.        )
  200.        {    /* Path lies entirely within clipping rectangle */
  201.         do_clip = 0;
  202.        }
  203.     else
  204.        {    /* Intersect the path box and the clip bounding box. */
  205.         /* If the intersection is empty, this fill is a no-op. */
  206.         gs_fixed_rect bbox;
  207.         bbox = pcpath->path.bbox;
  208.         if ( ibox.p.x >= bbox.q.x || ibox.p.y >= bbox.q.y ||
  209.             ibox.q.x <= bbox.p.x || ibox.q.y <= bbox.p.y
  210.            )
  211.            {    /* Intersection of boxes is empty! */
  212.             code = 0;
  213.             goto skip;
  214.            }
  215.         do_clip = 1;
  216.        }
  217. #undef ibox
  218.     sub_count = pfpath->subpath_count;
  219.     if ( !(code = alloc_line_list(&lst, sub_count)) )
  220.        {    gx_device *save_dev;
  221.         if ( (code = add_y_list(pfpath, &lst)) < 0 )
  222.             goto nope;
  223.         if ( do_clip )
  224.            {    /* Set up a clipping device. */
  225.             gx_device *dev = (gx_device *)&cdev;
  226.             save_dev = gs_currentdevice(pgs);
  227.             cdev = gs_clip_device;
  228.             cdev.target = save_dev;
  229.             cdev.list = pcpath->list;
  230.             gx_set_device_only(pgs, dev);
  231.             (*dev->procs->open_device)(dev);
  232.            }
  233.         code = fill_loop(pdevc, rule, &lst, pgs, adjust);
  234.         if ( do_clip )
  235.             gx_set_device_only(pgs, save_dev);
  236. nope:        free_line_list(&lst);
  237.        }
  238. skip:    if ( pfpath != ppath )    /* had to flatten */
  239.         gx_path_release(pfpath);
  240. #ifdef DEBUG
  241. if ( gs_debug['f'] || gs_debug['F'] )
  242.        {    dputs("[f]calls alloc   up   down  step slowx  iter  find  band bstep bfill\n");
  243.         dprintf4("   %5ld %5ld %5ld %5ld",
  244.             n_fill, n_fill_alloc, n_y_up, n_y_down);
  245.         dprintf4(" %5ld %5ld %5ld %5ld",
  246.             n_x_step, n_slow_x, n_iter, n_find_y);
  247.         dprintf3(" %5ld %5ld %5ld\n",
  248.             n_band, n_band_step, n_band_fill);
  249.        }
  250. #endif
  251.     return code;
  252. }
  253.  
  254. /* Create a line list for a (flattened) path. */
  255. /* We pass in the list size, so that we can use this to iterate */
  256. /* over more than one path simultaneously (needed for clipping). */
  257. private int
  258. alloc_line_list(ll_ptr ll, uint sub_count)
  259. {    ll->active_area = 0;
  260.     ll->close_count = sub_count;
  261.     ll->close_area =
  262.       (sub_count <= max_local_close ?
  263.        ll->local_close :
  264.        (line_close_segment *)gs_malloc(sub_count, sizeof(line_close_segment),
  265.                        "closing lines"));
  266.     ll->next_line = ll->close_area;
  267.     if ( ll->close_area == 0 )
  268.         return_error(gs_error_VMerror);
  269.     ll->next_active = ll->local_active;
  270.     ll->limit = ll->next_active + max_local_active;
  271.     ll->y_list = 0;
  272.     ll->y_line = 0;
  273.     stat(n_fill);
  274.     return 0;
  275. }
  276.  
  277. /* Free the line list */
  278. private void
  279. free_line_list(ll_ptr ll)
  280. {    line_close_segment *lp;
  281.     active_line *alp;
  282.     /* Splice out any inserted closing lines */
  283.     for ( lp = ll->close_area; lp != ll->next_line; lp++ )
  284.        {    segment *prev = lp->prev, *next = lp->next;
  285.         prev->next = next;
  286.         if ( next ) next->prev = prev;
  287.         lp->sub->last = prev;
  288.        }
  289.     /* Free any individually allocated active_lines. */
  290.     while ( (alp = ll->active_area) != 0 )
  291.        {    active_line *next = alp->alloc_next;
  292.         gs_free((char *)alp, 1, sizeof(active_line),
  293.             "active line");
  294.         ll->active_area = next;
  295.        }
  296.     /* Free any separately allocated closing lines. */
  297.     if ( ll->close_area != ll->local_close && ll->close_area != 0 )
  298.        {    gs_free((char *)ll->close_area, ll->close_count,
  299.             sizeof(line_close_segment), "closing lines");
  300.        }
  301. }
  302.  
  303. /* Construct a Y-sorted list of lines for a (flattened) path. */
  304. /* We assume the path is non-empty.  Only include non-horizontal */
  305. /* lines where one endpoint is locally Y-minimal. */
  306. private int
  307. add_y_list(gx_path *ppath, ll_ptr ll)
  308. {    register segment *pseg = (segment *)ppath->first_subpath;
  309.     subpath *psub;
  310.     segment *plast;
  311.     int first_dir, prev_dir, dir;
  312.     segment *prev;
  313.     /* fixed xmin = ll->box.p.x; */    /* not currently used */
  314.     fixed ymin = ll->box.p.y;
  315.     fixed xmax = ll->box.q.x;
  316.     fixed ymax = ll->box.q.y;
  317.     int code;
  318.  
  319.     while ( pseg )
  320.        {    switch ( pseg->type )
  321.            {    /* No curves left */
  322.         case s_start:
  323.             psub = (subpath *)pseg;
  324.             plast = psub->last;
  325.             dir = 2;    /* hack to skip first line */
  326.             if ( plast->type != s_line_close )
  327.                {    /* Create a fake s_line_close */
  328.                 line_close_segment *lp = ll->next_line++;
  329.                 segment *next = plast->next;
  330.                 lp->next = next;
  331.                 lp->prev = plast;
  332.                 plast->next = (segment *)lp;
  333.                 if ( next ) next->prev = (segment *)lp;
  334.                 lp->type = s_line_close;
  335.                 lp->pt = psub->pt;
  336.                 lp->sub = psub;
  337.                 plast = (segment *)lp;
  338.                 psub->last = plast;
  339.                }
  340.             break;
  341.         default:        /* s_line, _close */
  342.            {    fixed iy = pseg->pt.y;
  343.             fixed py = prev->pt.y;
  344.             /* Lines falling entirely outside the ibox */
  345.             /* are treated as though they were horizontal, */
  346.             /* i.e., they are never put on the list. */
  347. #define compute_dir(xo, xe, yo, ye)\
  348.   (xo > xmax && xe > xmax ? 0 :\
  349.    ye > yo ? (ye <= ymin || yo >= ymax ? 0 : dir_up) :\
  350.    ye < yo ? (yo <= ymin || ye >= ymax ? 0 : dir_down) :\
  351.    0)
  352. #define add_dir_lines(prev2, prev, this, pdir, dir)\
  353.   if ( pdir )\
  354.    { if ( (code = add_y_line(prev2, prev, pdir, ll)) < 0 ) return code; }\
  355.   if ( dir )\
  356.    { if ( (code = add_y_line(prev, this, dir, ll)) < 0 ) return code; }
  357.             dir = compute_dir(prev->pt.x, pseg->pt.x, py, iy);
  358.             if ( dir > prev_dir )
  359.                {    add_dir_lines(prev->prev, prev, pseg, prev_dir, dir);
  360.                }
  361.             else if ( prev_dir == 2 )    /* first line */
  362.                 first_dir = dir;
  363.             if ( pseg == plast )
  364.                {    /* We skipped the first segment of the */
  365.                 /* subpath, so the last segment must */
  366.                 /* receive special consideration. */
  367.                 /* Note that we have `closed' all subpaths. */
  368.                 if ( first_dir > dir )
  369.                    {    add_dir_lines(prev, pseg, psub->next, dir, first_dir);
  370.                    }
  371.                }
  372.            }
  373. #undef compute_dir
  374. #undef add_dir_lines
  375.            }
  376.         prev = pseg;
  377.         prev_dir = dir;
  378.         pseg = pseg->next;
  379.        }
  380.     return 0;
  381. }
  382. /* Internal routine to test a line segment and add it to the */
  383. /* pending list if appropriate. */
  384. private int
  385. add_y_line(const segment *prev_lp, const segment *lp, int dir, ll_ptr ll)
  386. {    gs_fixed_point this, prev;
  387.     register active_line *alp = ll->next_active;
  388.     fixed y_start;
  389.     if ( alp == ll->limit )
  390.        {    /* Allocate separately */
  391.         alp = (active_line *)gs_malloc(1, sizeof(active_line), "active line");
  392.         if ( alp == 0 ) return_error(gs_error_VMerror);
  393.         alp->alloc_next = ll->active_area;
  394.         ll->active_area = alp;
  395.         stat(n_fill_alloc);
  396.        }
  397.     else
  398.         ll->next_active++;
  399.     this.x = lp->pt.x;
  400.     this.y = lp->pt.y;
  401.     prev.x = prev_lp->pt.x;
  402.     prev.y = prev_lp->pt.y;
  403.     if ( (alp->direction = dir) > 0 )
  404.        {    /* Upward line */
  405.         y_start = prev.y;
  406.         set_al_points(alp, prev, this);
  407.         alp->pseg = lp;
  408.        }
  409.     else
  410.        {    /* Downward line */
  411.         y_start = this.y;
  412.         set_al_points(alp, this, prev);
  413.         alp->pseg = prev_lp;
  414.        }
  415.     /* Insert the new line in the Y ordering */
  416.        {    register active_line *yp = ll->y_line;
  417.         register active_line *nyp;
  418.         if ( yp == 0 )
  419.            {    alp->next = alp->prev = 0;
  420.             ll->y_list = alp;
  421.            }
  422.         else if ( y_start >= yp->start.y )
  423.            {    /* Insert the new line after y_line */
  424.             while ( stat(n_y_up), (nyp = yp->next) != NULL && y_start > nyp->start.y )
  425.                 yp = nyp;
  426.             alp->next = nyp;
  427.             alp->prev = yp;
  428.             yp->next = alp;
  429.             if ( nyp ) nyp->prev = alp;
  430.            }
  431.         else
  432.            {    /* Insert the new line before y_line */
  433.             while ( stat(n_y_down), (nyp = yp->prev) != NULL && y_start < nyp->start.y )
  434.                 yp = nyp;
  435.             alp->prev = nyp;
  436.             alp->next = yp;
  437.             yp->prev = alp;
  438.             if ( nyp ) nyp->next = alp;
  439.             else ll->y_list = alp;
  440.            }
  441.        }
  442.     ll->y_line = alp;
  443.     print_al("add ", alp);
  444.     return 0;
  445. }
  446.  
  447. /* Main filling loop.  Takes lines off of y_list and adds them to */
  448. /* x_list as needed. */
  449. private int
  450. fill_loop(const gx_device_color *pdevc, int rule, ll_ptr ll,
  451.   gs_state *pgs, fixed adjust)
  452. {    fixed adj2 = adjust << 1;
  453.     active_line *yll = ll->y_list;
  454.     gs_fixed_point pmax;
  455.     fixed y;
  456.     int code;
  457.     if ( yll == 0 ) return 0;        /* empty list */
  458.     pmax = ll->box.q;
  459.     y = yll->start.y;            /* first Y value */
  460.     ll->x_list = 0;
  461.     ll->x_head.x_current = min_fixed;    /* stop backward scan */
  462.     while ( 1 )
  463.        {    fixed y1, y0;
  464.         active_line *endp, *alp, *stopx;
  465.         fixed x;
  466.         int draw;
  467.         stat(n_iter);
  468.         /* Check whether we've reached the maximum y. */
  469.         if ( y >= pmax.y ) break;
  470.         /* Move newly active lines from y to x list. */
  471.         while ( yll != 0 && yll->start.y == y )
  472.            {    active_line *ynext = yll->next;    /* insert smashes next/prev links */
  473.             insert_x_new(yll, ll);
  474.             yll = ynext;
  475.            }
  476.         if ( ll->x_list == 0 )
  477.            {    /* No active lines, skip to next start */
  478.             if ( yll == 0 ) break;    /* no lines left */
  479.             y = yll->start.y;
  480.             continue;
  481.            }
  482.         /* Find the next evaluation point. */
  483.         /* Start by finding the smallest y value */
  484.         /* at which any currently active line ends */
  485.         /* (or the next to-be-active line begins). */
  486.         y1 = (yll != 0 ? yll->start.y : max_fixed);
  487.         for ( alp = ll->x_list; alp != 0; alp = alp->next )
  488.           if ( alp->end.y < y1 ) y1 = alp->end.y;
  489. #ifdef DEBUG
  490. if ( gs_debug['F'] )
  491.    {        dprintf2("[F]before loop: y=%f y1=%f:\n",
  492.                  fixed2float(y), fixed2float(y1));
  493.         print_line_list(ll->x_list);
  494.    }
  495. #endif
  496.         /* Now look for line intersections before y1. */
  497.         x = min_fixed;
  498.         y0 = y - adjust;
  499. #define have_pixels() (fixed_rounded(y0) < fixed_rounded(y1 + adjust))
  500.         draw = (have_pixels() ? 1 : -1);
  501.         /*
  502.          * Loop invariants:
  503.          *    alp = endp->next;
  504.          *    for all lines lp from stopx up to alp,
  505.          *      lp->x_next = al_x_at_y(lp, y1).
  506.          */
  507.         for ( alp = stopx = ll->x_list; stat(n_find_y), alp != 0;
  508.              endp = alp, alp = alp->next
  509.             )
  510.            {    fixed nx = al_x_at_y(alp, y1);
  511.             fixed dx_old, dx_den;
  512.             /* Check for intersecting lines. */
  513.             if ( nx >= x )
  514.                 x = nx;
  515.             else if
  516.                ( draw >= 0 && /* don't bother if no pixels */
  517.                  (dx_old = alp->x_current - endp->x_current) >= 0 &&
  518.                  (dx_den = dx_old + endp->x_next - nx) > dx_old
  519.                )
  520.                {    /* Make a good guess at the intersection */
  521.                 /* Y value using only local information. */
  522.                 fixed dy = y1 - y, y_new;
  523.                 if_debug3('f', "[f]cross: dy=%g, dx_old=%g, dx_new=%g\n",
  524.                   fixed2float(dy), fixed2float(dx_old),
  525.                   fixed2float(dx_den - dx_old));
  526.                 /* Do the computation in single precision */
  527.                 /* if the values are small enough. */
  528.                 y_new =
  529.                   ((dy | dx_old) < 1L << (size_of(fixed)*4-1) ?
  530.                    dy * dx_old / dx_den :
  531.                    fixed_mult_quo(dy, dx_old, dx_den))
  532.                   + y;
  533.                 /* The crossing value doesn't have to be */
  534.                 /* very accurate, but it does have to be */
  535.                 /* greater than y and less than y1. */
  536.                 if_debug3('f', "[f]cross y=%g, y_new=%g, y1=%g\n",
  537.                   fixed2float(y), fixed2float(y_new),
  538.                   fixed2float(y1));
  539.                 stopx = alp;
  540.                 if ( y_new <= y ) y_new = y + 1;
  541.                 if ( y_new < y1 )
  542.                    {    y1 = y_new;
  543.                     nx = al_x_at_y(alp, y1);
  544.                     draw = 0;
  545.                    }
  546.                 if ( nx > x ) x = nx;
  547.                }
  548.             alp->x_next = nx;
  549.            }
  550.         /* Recompute next_x for lines before the intersection. */
  551.         for ( alp = ll->x_list; alp != stopx; alp = alp->next )
  552.             alp->x_next = al_x_at_y(alp, y1);
  553. #ifdef DEBUG
  554. if ( gs_debug['F'] )
  555.    {        dprintf1("[F]after loop: y1=%f\n", fixed2float(y1));
  556.         print_line_list(ll->x_list);
  557.    }
  558. #endif
  559.         /* Fill a multi-trapezoid band for the active lines. */
  560.         /* Don't bother if no pixel centers lie within the band. */
  561.         if ( draw > 0 || draw == 0 && have_pixels() )
  562.            {    active_line *alp = ll->x_list;
  563.             fixed height = y1 - y + adj2;
  564.             fixed xlbot, xltop;    /* as of last "outside" line */
  565.             int inside = 0;
  566.             stat(n_band);
  567.             x = min_fixed;
  568.             /* rule = -1 for winding number rule, i.e. */
  569.             /* we are inside if the winding number is non-zero; */
  570.             /* rule = 1 for even-odd rule, i.e. */
  571.             /* we are inside if the winding number is odd. */
  572.             /* Clever, eh? */
  573. #define inside_path_p() (inside & rule)
  574.             while ( alp != 0 )
  575.                {    fixed xbot = alp->x_current;
  576.                 fixed xtop = alp->x_next;
  577.                 print_al("step", alp);
  578.                 stat(n_band_step);
  579.                 if ( inside_path_p() )
  580.                  { inside += alp->direction;
  581.                    if ( !inside_path_p() )    /* about to go out */
  582.                     {    fixed wbot = xbot - xlbot + adj2;
  583.                     fixed wtop = xtop - xltop + adj2;
  584.                     stat(n_band_fill);
  585.                     /* If lines are temporarily out of */
  586.                     /* order, wtop might be negative. */
  587.                     /* Patch this up now. */
  588.                     if ( wtop < 0 )
  589.                        {    xltop += wtop >> 1;
  590.                         wtop = 0;
  591.                        }
  592.                     code = gz_fill_trapezoid_fixed(
  593.                          xlbot - adjust,
  594.                          wbot, y0,
  595.                          xltop - adjust, wtop,
  596.                          height, 0, pdevc, pgs);
  597.                     if ( code < 0 ) return code;
  598.                     }
  599.                  }
  600.                 else            /* outside */
  601.                    {    inside += alp->direction;
  602.                     if ( inside_path_p() )    /* about to go in */
  603.                         xlbot = xbot, xltop = xtop;
  604.                    }
  605.                 alp = alp->next;
  606.                }
  607.            }
  608.         code = update_x_list(ll->x_list, y1);
  609.         if ( code < 0 ) return code;
  610.         y = y1;
  611.        }
  612.     return 0;
  613. }
  614.  
  615. /* Insert a newly active line in the X ordering. */
  616. private void
  617. insert_x_new(active_line *alp, ll_ptr ll)
  618. {    register active_line *next;
  619.     register active_line *prev = &ll->x_head;
  620.     register fixed x = alp->start.x;
  621.     alp->x_current = x;
  622.     while ( stat(n_x_step),
  623.         (next = prev->next) != 0 && x_precedes(next, alp, x)
  624.            )
  625.         prev = next;
  626.     alp->next = next;
  627.     alp->prev = prev;
  628.     if ( next != 0 ) next->prev = alp;
  629.     prev->next = alp;
  630. }
  631.  
  632. /* Clean up after a pass through the main loop. */
  633. /* If any lines are out of order, re-sort them now. */
  634. /* Also drop any ended lines. */
  635. private int
  636. update_x_list(active_line *x_first, fixed y1)
  637. {    fixed x;
  638.     register active_line *alp;
  639.     active_line *nlp;
  640.     for ( x = min_fixed, alp = x_first; alp != 0; alp = nlp )
  641.        {    fixed nx = alp->x_current = alp->x_next;
  642.         nlp = alp->next;
  643.         if_debug4('f', "[f]check %lx,x=%g %lx,x=%g\n",
  644.               (ulong)alp->prev, fixed2float(x),
  645.               (ulong)alp, fixed2float(nx));
  646.         if ( alp->end.y == y1 )
  647.            {    /* Handle a line segment that just ended. */
  648.             const segment *pseg = alp->pseg;
  649.             const segment *next;
  650.             gs_fixed_point npt;
  651.             /*
  652.              * The computation of next relies on the fact that
  653.              * all subpaths have been closed.  When we cycle
  654.              * around to the other end of a subpath, we must be
  655.              * sure not to process the start/end point twice.
  656.              */
  657.             next =
  658.               (alp->direction == dir_up ?
  659.                (/* Upward line, go forward along path. */
  660.                 pseg->type == s_line_close ? /* end of subpath */
  661.                  ((line_close_segment *)pseg)->sub->next :
  662.                  pseg->next) :
  663.                (/* Downward line, go backward along path. */
  664.                 pseg->type == s_start ? /* start of subpath */
  665.                  ((subpath *)pseg)->last->prev :
  666.                  pseg->prev)
  667.               );
  668.             npt.y = next->pt.y;
  669.             if_debug5('F', "[F]ended %lx: pseg=%lx y=%f next=%lx npt.y=%f\n",
  670.                  (ulong)alp, (ulong)pseg, fixed2float(pseg->pt.y),
  671.                  (ulong)next, fixed2float(npt.y));
  672.             if ( npt.y <= pseg->pt.y )
  673.                {    /* End of a line sequence */
  674.                 alp->prev->next = nlp;
  675.                 if ( nlp ) nlp->prev = alp->prev;
  676.                 if_debug1('F', "[F]drop %lx\n", (ulong)alp);
  677.                 continue;
  678.                }
  679.             else
  680.                {    alp->pseg = next;
  681.                 npt.x = next->pt.x;
  682.                 set_al_points(alp, alp->end, npt);
  683.                 print_al("repl", alp);
  684.                }
  685.            }
  686.         if ( nx <= x )
  687.            {    /* Move alp backward in the list. */
  688.             active_line *prev = alp->prev;
  689.             active_line *next = nlp;
  690.             prev->next = next;
  691.             if ( next ) next->prev = prev;
  692.             while ( !x_precedes(prev, alp, nx) )
  693.                {    if_debug2('f', "[f]swap %lx,%lx\n",
  694.                          (ulong)alp, (ulong)prev);
  695.                 next = prev, prev = prev->prev;
  696.                }
  697.             alp->next = next;
  698.             alp->prev = prev;
  699.             /* next might be null, if alp was in */
  700.             /* the correct spot already. */
  701.             if ( next ) next->prev = alp;
  702.             prev->next = alp;
  703.            }
  704.         else
  705.             x = nx;
  706.        }
  707. #ifdef DEBUG
  708. if ( gs_debug['f'] || gs_debug['F'] )
  709.     for ( alp = x_first; alp != 0; alp = alp->next )
  710.      if ( alp->next != 0 && alp->next->x_current < alp->x_current )
  711.        {    lprintf("[f]Lines out of order!\n");
  712.         print_active_line("   1:", alp);
  713.         print_active_line("   2:", alp->next);
  714.         return_error(gs_error_Fatal);
  715.        }
  716. #endif
  717.     return 0;
  718. }
  719.